package com.andong.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.andong.domain.entity.Admin;
import com.andong.domain.entity.SubjectVideo;
import com.andong.domain.pojo.PageResult;
import com.andong.domain.pojo.Result;
import com.andong.domain.vo.AdminListVO;
import com.andong.utils.AliOSSUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.andong.domain.entity.Notification;
import com.andong.service.NotificationService;
import com.andong.mapper.NotificationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
* @author 王海云
* @description 针对表【notification】的数据库操作Service实现
* @createDate 2024-02-22 18:39:00
*/
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification>
    implements NotificationService{
    @Resource
    private AliOSSUtils aliOSSUtils;

    @Override
    public PageResult queryList(Integer page, Integer pageSize, String name, LocalDate begin, LocalDate end) {
        // 构造Admin分页器
        Page<Notification> notificationPage = new Page<>(page, pageSize);
        // 构造Admin条件查询器, 得到Admin分页查询基本信息结果
        notificationPage = lambdaQuery()
                .like(name != null, Notification::getName, name)
                .ge(begin != null, Notification::getCreateTime, begin)
                .le(end != null, Notification::getCreateTime, end)
                .orderByDesc(Notification::getUpdateTime)
                .page(notificationPage);
        return new PageResult(notificationPage.getTotal(), notificationPage.getRecords());
    }

    @Override
    public Result saveOrModify(Notification notification) {
        if (notification.getId() != null) {
            // 修改
            // 判断是否修改了文件
            String oldUrl = lambdaQuery()
                    .eq(Notification::getId, notification.getId())
                    .one()
                    .getUrl();
            if (!oldUrl.equals(notification.getUrl())) {
                // 删除阿里云文件
                aliOSSUtils.delete(oldUrl);
            }
            updateById(notification);
            return Result.success();
        }
        // 新增
        save(notification);
        return Result.success();
    }

    @Override
    public Result deleteByIds(List<Long> ids) {
        // 删除阿里云视频
        List<Notification> list = lambdaQuery()
                .in(Notification::getId, ids)
                .list();
        list.forEach(notification -> {
            if (Objects.nonNull(notification.getUrl())) {
                aliOSSUtils.delete(notification.getUrl());
            }
        });
        // 删除题目
        removeByIds(ids);
        return Result.success();
    }
}




